﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Data.Common;

namespace GestorSQL.DataBase.Interface
{
    public abstract class OleDbDataBaseProvider : IDataBaseProvider
    {
        private string _lastResultColumnName = null;

        protected OleDbConnection oleDbConnection = null;
        protected OleDbCommand oleDbCommand = null;
        protected OleDbDataReader oleDbDataReader = null;
        protected OleDbTransaction oleDbTransaction = null;

        #region Contructor
        public OleDbDataBaseProvider()
        {
            oleDbConnection = new OleDbConnection();
        }
        #endregion

        #region IDataBaseProvider Members
        public virtual string ConnectionString
        {
            get { return oleDbConnection.ConnectionString; }
            set { oleDbConnection.ConnectionString = value; }
        }

        public bool OmitFirstResult { get { return true; } }

        public bool OmitLastResult { get { return true; } }

        public string LastResultColumnName
        {
            get
            {
                if (_lastResultColumnName == null)
                    _lastResultColumnName = string.Format("GestorSQLFin{0}", Guid.NewGuid().ToString().Replace("-", "")).PadLeft(30, 'W').Substring(0, 30).ToUpper();
                return _lastResultColumnName;
            }
        }

        public virtual IDataReader Execute(string query)
        {
            Open();

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("select 1");
            sb.AppendLine(query);
            sb.AppendLine(string.Format("select 1 as {0}", LastResultColumnName));

            oleDbCommand = new OleDbCommand(sb.ToString(), oleDbConnection);
            if (oleDbTransaction != null)
                oleDbCommand.Transaction = oleDbTransaction;            

            oleDbDataReader = oleDbCommand.ExecuteReader();

            return oleDbDataReader;
        }

        public virtual void BeginTransction()
        {
            if (oleDbConnection != null)
                oleDbTransaction = oleDbConnection.BeginTransaction();
        }

        public virtual void CommitTransaction()
        {
            if (oleDbTransaction != null)
                oleDbTransaction.Commit();
        }

        public virtual void RollbackTransaction()
        {
            if (oleDbTransaction != null)
                oleDbTransaction.Rollback();
        }

        public virtual void Open()
        {
            if (oleDbConnection != null && oleDbConnection.State == ConnectionState.Closed)
                oleDbConnection.Open();
        }

        public virtual void Close()
        {
            if (oleDbTransaction != null)
                oleDbTransaction.Dispose();

            if (oleDbCommand != null)
            {
                oleDbCommand.Cancel();
                oleDbCommand.Dispose();
            }

            if (oleDbDataReader != null)
                oleDbDataReader.Close();

            if (oleDbConnection != null && oleDbConnection.State == ConnectionState.Open)
                oleDbConnection.Close();
        }

        #endregion

        #region IDisposable Members

        public virtual void Dispose()
        {
            Close();

            if (oleDbDataReader != null)
                oleDbDataReader.Dispose();

            if (oleDbCommand != null)
                oleDbCommand.Dispose();

            if (oleDbConnection != null)
                oleDbConnection.Dispose();

            oleDbConnection = null;
            oleDbCommand = null;
            oleDbDataReader = null;
        }

        #endregion
    }
}
